<?php

class Admin_Table_Auction_Attaches extends Apk_Db_Table {

    protected $_name = 'auction_attaches';
    protected $_primary = 'ID';
    public $_ERROR = "";

    public function GetError() {
        return $this->_ERROR;
    }

    public function getDataList($id_lot, $page = 1, $sort = null, $page_count = null, $filters = array()) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name, array('id', 'file_name' => "name"))
                ->joinLeft("dir_attaches_type", "dir_attaches_type.id = ".$this->_name.".type", array('file_type' => "dir_attaches_type.name"))
                ->where("id_lot = ?", $id_lot)
                ->where("is_removed = ?", 0);
                //->order($this->_name.".id");

    	if (!empty($filters)) {
    		foreach ($filters as $k => $v){
    			switch ($k) {
    				case 'by_file_type':
    					if(intval($v)>0) $sel->where($this->_name.".type = ?", $v);
    					break;
    			}
    		}
        }

    	$pieces = preg_split("/\s/u", $sort);
        switch ($pieces[0]) {
            case 'file_type':
                $sel->order("dir_attaches_type.name " . $pieces[1]);
                break;
            case 'file_name':
                $sel->order($this->_name.".name " . $pieces[1]);
                break;
            default:
                $sel->order($this->_name.".id");
                break;
        }
        
        return $this->getPager($sel, $page, array('perPage' => $page_count));
    }

    public function getFileData($id) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name, array('name', 'file_path', 'file_name'))
                ->where("id = ?", $id);

        $data = $this->getList($sel)->toArray();

        if (!empty($data)) $data = $data[0];

        return $data;
    }
    public function getVideoFile($id_lot) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name, array('video_path' => 'file_path' , 'video_name'  => 'file_name'))
                ->where($this->_name.'.type = ?', '7')
                ->where($this->_name.'.id_lot = ?', $id_lot)
                ->where("is_removed = ?", 0)
                ->order($this->_name.'.modify_date');

        $data = $this->getList($sel)->toArray();

        if (!empty($data)) $data = $data[0];

        return $data;
    }

    public function getFileTypes() {
        $sel = $this->getAdapter()->select()
                ->from('dir_attaches_type', array('id', 'name' => "shortname"))
                ->where('private = ?',0)
                ->order("id");

        return $this->getList($sel)->toArray();
    }

    public function getLotFilesCount($lot_id) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name, array('files_count' => "count(*)"))
                ->where("is_removed = ?", 0)
                ->where("id_lot = ?", $lot_id);

        return $this->getList($sel)->getRow(0)->toArray();
    }

    public function getLotFilesIDs($lot_id) {
        $sel = $this->getAdapter()->select()
                ->from($this->_name, array('id'))
                ->where("is_removed = ?", 0)
                ->where("id_lot = ?", $lot_id);

        return $this->getList($sel)->toArray();
    }

    public function insert(array $data, $user_id) {
        $data['add_user_id'] = $user_id;
        try {
            parent::insert($data);
            return $this->getAdapter()->lastInsertId($this->_name);
        } catch (Exception $e) {
            return false;
        }
    }

    public function update(array $data, $id) {
        try {
            return parent::update($data, 'id = ' . $id);
        } catch (Exception $e) {
            return false;
        }
    }

    public function remove(array $items, $user_id) {
        foreach ($items as $id) {
            $data = array();
            $data['modify_user_id'] = $user_id;
            $data['modify_date'] = new Zend_Db_Expr("now()");
            $data['is_removed'] = 1;

            parent::update($data, "id = " . $id);
        }
        return true;
    }
}
